﻿#include "mainwindowh.h"
#include "stateserver.h"
#include <QThread>

MainWindowh::MainWindowh(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_state = new StateServer(54611);
	m_th = new QThread();
	m_state->moveToThread(m_th);
	m_th->start();
	QMetaObject::invokeMethod(m_state, "Start", Qt::QueuedConnection);

	InitState();

}

MainWindowh::~MainWindowh()
{
}
bool MainWindowh::InitState()
{
	m_info.joint = { 21.2, 31.2, 41.2, 42.2, 43.2, 44.2 };
	m_info.pos = { 1259.1, -47.85, 2200.01, -90.0, 0.0, 180.0 };
	m_info.DI = "0000111101010000001111000001001";
	m_info.DO = "000000000000000000000000000000";
	m_info.falut = 0;
	m_info.move = 0;
	m_info.pointId = 0;
	m_info.tools = 0;
	m_state->SetStateData(StateTransToByte());
	return true;
}

QByteArray MainWindowh::StateTransToByte()
{
	QByteArray data;
	data.append(QByteArray::number(m_info.joint.joint1));
	data.append(",");
	data.append(QByteArray::number(m_info.joint.joint2));
	data.append(",");
	data.append(QByteArray::number(m_info.joint.joint3));
	data.append(",");
	data.append(QByteArray::number(m_info.joint.joint4));
	data.append(",");
	data.append(QByteArray::number(m_info.joint.joint5));
	data.append(",");
	data.append(QByteArray::number(m_info.joint.joint6));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.x));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.y));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.z));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.a));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.b));
	data.append(",");
	data.append(QByteArray::number(m_info.pos.c));
	data.append(",");

	data.append(QByteArray::number(m_info.pointId));
	data.append(",");

	data.append(QByteArray::number(m_info.move));
	data.append(",");
	data.append(QByteArray::number(m_info.tools));
	data.append(",");

	data.append(QByteArray::number(m_info.falut));
	data.append(",");

	data.append(m_info.DI.toUtf8());
	data.append(",");

	data.append(m_info.DO.toUtf8());
	data.append(",");
	return data;
}


QByteArray MainWindowh::CommandProcessor(const QByteArray& data)
{
	return "";
}
